//
//  BaseNaviViewController.m
//  YUXiu
//
//  Created by 王竞成 on 16/6/22.
//  Copyright © 2016年 Wang. All rights reserved.
//

#import "BaseNaviViewController.h"

@interface BaseNaviViewController ()

@end

@implementation BaseNaviViewController

+ (void)initialize {
    // 设置NavigationBar的主题
    [self setupNavigationBarTheme];
    // 设置UIBarButtonItem的主题
    [self setupBarButtonItemTheme];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark -
#pragma mark - UI操作
/** 设置NavigationBar的主题 */
+ (void)setupNavigationBarTheme
{
    UINavigationBar *navigationBar = [UINavigationBar appearance];
    
    // 设置导航栏背景
    navigationBar.barTintColor = kNaviBarCOLOR;
    navigationBar.tintColor = kOrangeCOLOR;
    
    // 设置文字属性
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = kBlackCOLOR;
    textAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:17];
    
    [navigationBar setTitleTextAttributes:textAttrs];
}

/** 设置UIBarButtonItem的主题 */
+ (void)setupBarButtonItemTheme
{
    // 设置整个项目所有item的主题样式
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    
    // 设置普通状态
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    // 设置不可用状态
    NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionary];
    disableTextAttrs[NSFontAttributeName] = textAttrs[NSFontAttributeName];
    [item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
}

/**
 *  @author frank.chan, 16-01-22 14:01:48
 *
 *  重写这个方法的目的：能够拦截所有push进来的控制器
 *
 *  @param viewController 即将push进来的控制器
 *  @param animated       是否需要动画
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) { // 这时push进来的控制器，不是第一个子控制器（不是根控制器）
        // 自动显示和隐藏tabbar
        viewController.hidesBottomBarWhenPushed = YES;
        
     
        // 设置左边的返回按钮,若嫌系统箭头不好看，可在此设置返回item图片
//        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(back) image:@"inform_nav_icn_return_normal" highImage:@"inform_nav_icn_return_pressed"];
        
        UIButton *left = [[UIButton alloc]initWithFrame:CGRectMake(0, 15, 50, 22)];
        [left setBackgroundImage:[UIImage imageNamed:@"back-1"] forState:UIControlStateNormal];
        [left addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithCustomView:left];
        if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0) {
            UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            negativeSpacer.width = -10;
            self.navigationItem.leftBarButtonItems = @[negativeSpacer,leftButton];
        }else{
            self.navigationItem.leftBarButtonItem = leftButton;
            
        }


    }
    [super pushViewController:viewController animated:animated];
}

+(void)setNavigationLeftIBacktarget:(nullable id)target action:(nullable SEL)action {
    UIViewController *viewController = target;
    UIBarButtonItem*leftItem=[UIBarButtonItem itemWithTarget:target action:action image:@"inform_nav_icn_return_normal" highImage:@"inform_nav_icn_return_pressed"];

    viewController.navigationItem.leftBarButtonItems =[NSArray arrayWithObjects:leftItem, nil];
}

/**
 *  @author frank.chan, 16-01-22 14:01:59
 *
 *  点击左边的返回按钮
 */
- (void)back
{
    // 因为self本来就是一个导航控制器，在这里self.navigationController指的是nil，所以用self，而不是self.navigationController
    if (![self popViewControllerAnimated:YES]) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
